package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.*;
import com.heima.pojo.dto.NodeSaveDto;
import com.heima.pojo.entity.*;
import com.heima.pojo.vo.NodeRegionBusinessVo;
import com.heima.pojo.vo.VmTypeNodeRegionVo;
import com.heima.pojo.vo.PageVo;
import com.heima.service.NodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;

    /**
     * 点位搜索 分页条件查询
     * @param pageIndex
     * @param pageSize
     * @param name
     * @param regionId
     * @return
     */
    @Override
    public PageVo<NodeRegionBusinessVo> getNodePage(Integer pageIndex, Integer pageSize, String name, String regionId) {
        //先获取非集合的几个数据并进行封装
        PageVo<NodeRegionBusinessVo> nodePageVo = new PageVo<>();

        PageHelper.startPage(pageIndex,pageSize);
        //进行分页和查询 点位表
        //  构建查询条件（封装为Map或实体类传递给Mapper）
        HashMap<String, Object> params = new HashMap<>();
         params.put("name",name);
         params.put("regionId",regionId);

        List<NodeRegionBusinessVo> list = nodeMapper.getBynameRegionId(params);

        Page<NodeRegionBusinessVo> page = (Page<NodeRegionBusinessVo>) list;
        nodePageVo.setPageSize(pageSize);
        nodePageVo.setPageIndex(pageIndex);
        nodePageVo.setTotalPage(page.getPages());
        nodePageVo.setTotalCount(page.getTotal());
        nodePageVo.setCurrentPageRecords(list);

        return nodePageVo;

//        for (Node node1 : node) {
//            NodeRegionBusinessVo nodeRegionBusinessVo = new NodeRegionBusinessVo();
//            BeanUtils.copyProperties(node1, nodeRegionBusinessVo);
//            //查询region表
//            Region region = regionMapper.selectById(node1.getRegionId());
//            nodeRegionBusinessVo.setRegion(region);
//            //查询business表
//            Business business = businessMapper.selectById(node1.getBusinessId());
//            nodeRegionBusinessVo.setBusinessType(business);
//            //设置vmcode
//            LambdaQueryWrapper<VendingMachine> qw = new LambdaQueryWrapper<>();
//            qw.eq(VendingMachine::getNodeId,node1.getId());
//            List<VendingMachine> vendingMachines = vendingMachineMapper.selectList(qw);
//            nodeRegionBusinessVo.setVmCount(vendingMachines.size());
//            nodeRegionBusinessVoList.add(nodeRegionBusinessVo);
//        }
//        //封装数据到返回对象
//        nodePageVo.setCurrentPageRecords(nodeRegionBusinessVoList);

//        return nodePageVo;
    }

    /**
     * 新增点位
     * @param nodeSaveDto
     * @return
     */
    @Override
    public Boolean save(NodeSaveDto nodeSaveDto) {
        Node node = new Node();
        BeanUtils.copyProperties(nodeSaveDto,node);
        node.setUpdateTime(LocalDateTime.now());
        node.setCreateTime(LocalDateTime.now());
        int insert = nodeMapper.insert(node);
        if (insert==1){
            return true;
        }
        return false;
    }

    /**
     * 修改点位
     * @param id
     * @param node
     * @return
     */
    @Override
    public Boolean update(Long id, Node node) {
        node.setId(id);
        int i = nodeMapper.updateById(node);
        if (i==1){
            return true;
        }
        return false;
    }

    /**
     * 删除点位
     * @param id
     * @return
     */
    @Override
    public Boolean delete(String id) {
        int i = nodeMapper.deleteById(id);
        if (i==1){
            return true;
        }
        return false;
    }

    /**
     * 通过id查询点位详情
     * @return
     */
    @Override
    public List<VmTypeNodeRegionVo> getNodeDetail(Long id) {
        List<VmTypeNodeRegionVo> vmTypeNodeRegionVos = new ArrayList<>();
        //查询vending-machine表
        LambdaQueryWrapper<VendingMachine> qw = new LambdaQueryWrapper<>();
        qw.eq(VendingMachine::getNodeId,id);
        List<VendingMachine> vendingMachines = vendingMachineMapper.selectList(qw);
        int count = vendingMachines.size();
        for (VendingMachine vendingMachine : vendingMachines) {
            VmTypeNodeRegionVo vmTypeNodeRegionVo = new VmTypeNodeRegionVo();
            BeanUtils.copyProperties(vendingMachine, vmTypeNodeRegionVo);
            //查询vm-type表
            LambdaQueryWrapper<TbVmType> vmType = new LambdaQueryWrapper<>();
            vmType.eq(TbVmType::getTypeId,vendingMachine.getVmType());
            TbVmType tbVmType = vmTypeMapper.selectOne(vmType);
            vmTypeNodeRegionVo.setType(tbVmType);
            //查询node对象
            Node node = nodeMapper.selectById(id);
            //获取currentPageRecords集合
            NodeRegionBusinessVo nodeRegionBusinessVo = new NodeRegionBusinessVo();
            BeanUtils.copyProperties(node, nodeRegionBusinessVo);
            //设置vmCount
            nodeRegionBusinessVo.setVmCount(count);
            //查询region表
            Region region = regionMapper.selectById(node.getRegionId());
            nodeRegionBusinessVo.setRegion(region);
            //查询business表
            Business business = businessMapper.selectById(node.getBusinessId());
            nodeRegionBusinessVo.setBusinessType(business);
            //封装数据
            vmTypeNodeRegionVo.setNode(nodeRegionBusinessVo);
            //获取region实体类
            Region r = regionMapper.selectById(node.getRegionId());
            //封装数据
            vmTypeNodeRegionVo.setRegion(r);
            //把对象放入返回的集合中
            vmTypeNodeRegionVos.add(vmTypeNodeRegionVo);
        }

        return vmTypeNodeRegionVos;

    }

    /**
     * 获取点位数量
     * @return
     */
    @Override
    public Integer getNodeCount() {
        List<Node> nodes = nodeMapper.selectList( new LambdaQueryWrapper<>());
        Integer count = nodes.size();
        return count;
    }


}



















